package com.aop.simple;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @description 当前Action主要是为了处理测试aop的几种处理类型
 * 前置，环绕，后置，后置返回通知，异常通知
 * @Author longdeng
 * @Date 2020/8/26 0026 22:55
 */
@Controller
@RequestMapping("/api/aop/simple")
public class SimpleAopAction extends BaseAction {


    /**
     * @return
     * @description 简单测试SimpleAopAction
     * 正常流程，没有异常出现
     * @author longdeng
     */
    @RequestMapping("/simpleAction")
    @ResponseBody
    public String simpleAction() {
        getDto().put("name", "zhouxl");
        return "success";
    }

    /**
     * @return
     * @description 出现异常的抛出
     * @author longdeng
     */
    @RequestMapping("afterThrow")
    @ResponseBody
    public String afterThrow() {
        int num = 1 / 0;
        return "error";
    }

    /**
     * @param name
     * @return
     * @description 将name值put到dto中，看能否再get出来
     * @author longdeng
     */
    @RequestMapping("putValue")
    @ResponseBody
    public String putValue(@RequestParam("name") String name) {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = attrs.getRequest();
        request.setAttribute("name", name);
        return "putValue";
    }

    /**
     * @return
     * @description 从dto中拿出name值
     * @author longdeng
     */
    @RequestMapping("getValue")
    @ResponseBody
    public String getValue() {
        return "getValue";
    }


}
